﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public partial class AddContact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            
        }
         lab_membername.Text = Session["name"].ToString();


    }
    protected void SaveBtn_Click(object sender, EventArgs e)
    {
         string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("Images/"+filename));

          SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString.ToString());
          SqlCommand cmd = new SqlCommand("insert into Contact (Name,Age,PhoneNo,Email,Location,Gender,BirthDay,Address,ImageName,ImagePath,MemberName) values(@Name,@Age,@PhoneNo,@Email,@Location,@Gender,@BirthDay,@Address,@ImageName,@ImagePath,@MemberName)", con);
          cmd.Parameters.AddWithValue("@Name", NameText.Text);
          cmd.Parameters.AddWithValue("@Age", AgeText.Text);
          cmd.Parameters.AddWithValue("@PhoneNo", PhoneText.Text);
          cmd.Parameters.AddWithValue("@Email", EmailText.Text);
          cmd.Parameters.AddWithValue("@Location",LocationText.Text);
          cmd.Parameters.AddWithValue("@Gender", GenderDDL.SelectedItem.Text);
          cmd.Parameters.AddWithValue("@BirthDay", BirthDay.Text);
          cmd.Parameters.AddWithValue("@Address", AddressText.Text);
          cmd.Parameters.AddWithValue("@ImageName",filename);
          cmd.Parameters.AddWithValue("@ImagePath", "Images/"+filename);
          cmd.Parameters.AddWithValue("@MemberName", lab_membername.Text);
          con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
   
        
    }
    protected void NameText_TextChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString.ToString());
        SqlCommand cmd = new SqlCommand("select * from Contact where Name='"+NameText.Text+"'", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            Label1.Text = "User Name is Already Exist";
            //this.Label1.ForeColor = ;

        }
        else
        {
            Label1.Text = "UserName is Available";

            //this.Label1.ForeColor = Color.Red;
        }
        con.Close();
    }
}